package cn.ad.server.job.adapter.persistence.job;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import cn.ad.server.job.adapter.dbaccess.DbDataContainer;
import cn.ad.server.job.domain.job.AdTemplateWarmUpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import cn.ad.server.common.Constants;
import cn.ad.server.job.adapter.dbaccess.AssembledCachedDataContainer;
import cn.ad.server.domain.ad.AdTemplate;
import cn.ad.server.domain.ad.PlatformAdspaceMap;
import cn.ad.server.domain.ad.PlatformTagSizeMap;
import cn.ad.server.domain.ad.PlatformTagTemplateMap;

@Repository
public class AdTemplateWarmUpServant implements AdTemplateWarmUpService {

	private static Logger logger = LoggerFactory.getLogger(AdTemplateWarmUpServant.class);

	@Resource(name = "cacheRedisTemplate")
	private ValueOperations<String, Integer> integerValueOperations;

	@Resource(name = "cacheRedisTemplate")
	private HashOperations<String, String, AdTemplate> hashValueOperations;

	@Resource(name = "cacheRedisTemplate")
	private RedisTemplate<String, String> redisTemplate;
	
	@Resource(name = "cacheRedisTemplate")
	private ValueOperations<String, PlatformTagSizeMap> platformTagSizeMapValueOperations;
	 
	@Resource(name = "cacheRedisTemplate")
	private ValueOperations<String, PlatformAdspaceMap> platformAdspaceMapValueOperations;

	@Autowired
	private AssembledCachedDataContainer container;

	@Override
	public void warmUpTagId() {
		List<PlatformTagTemplateMap> tagMaplist = DbDataContainer.getPlatformTagTemplateMapAll();// 根据所有的数据
		if (tagMaplist == null) {
			return;
		}
		ConcurrentHashMap<String, Integer> tagIdAdTemplateIdMap = container.getTagIdAdTemplateIdMap();
		for (PlatformTagTemplateMap am : tagMaplist) {
			try {
				if (am.getStatus() == 1) {
					tagIdAdTemplateIdMap.put(am.getTagId(), am.getTemplateId());
					//upToRedis(am.getTagId(), am.getTemplateId(), true);
					upToRedis2(am.getTagId(), am.getTemplateId(), true);
				} else if (am.getStatus() != 1) {
					tagIdAdTemplateIdMap.remove(am.getTagId());
					//upToRedis(am.getTagId(), am.getTemplateId(), false);
					upToRedis2(am.getTagId(), am.getTemplateId(), false);
				}

			} catch (Exception e) {
				if (am != null) {
					logger.error("错误：在获取PlatformTagTemplateMap_id =" + am.getId() + "数据时发生错误 \n 错误信息：", e);
				}
			}
		}
	}

	@Value("${cached.redisDebug:false}")
	private boolean redisDebug;

	void upToRedis(String tagId, Integer adm, boolean operatorFlg) {
		if (!redisDebug) {
			return;
		}
		String key = "MapTagIdToAdTemplateId:" + tagId + ":" + adm;
		if (operatorFlg) {
			integerValueOperations.set(key, adm);
		} else {
			redisTemplate.delete(key);
		}
	}

	void upToRedis2(String tagId, Integer adTemplateId, boolean operatorFlg) {
		if (operatorFlg) {
			AdTemplate adTemplate = DbDataContainer.getAdTemplate(adTemplateId);
			if (adTemplate != null) {
				hashValueOperations.put(Constants.TAG_TABLE_REDIS, tagId, adTemplate);
				return;
			}
			hashValueOperations.delete(Constants.TAG_TABLE_REDIS, tagId);
		}
	}

	@Override
	public void warmUpTagSize() {
		List<PlatformTagSizeMap> tagMaplist = DbDataContainer.getPlatformTagSizeMapAll();// 根据所有的数据
		if (tagMaplist == null) {
			return;
		}
		for (PlatformTagSizeMap am : tagMaplist) {
			try {
				if (am.getStatus() == 1) {
					platformTagSizeMapValueOperations.set(am.toCacheTagIdKey(), am);
				} else if (am.getStatus() != 1) {
					redisTemplate.delete(am.toCacheTagIdKey());
				}
			} catch (Exception e) {
				if (am != null) {
					logger.error("错误：在获取PlatformTagSizeMap_id =" + am.getId() + "数据时发生错误 \n 错误信息：", e);
				}
			}
		}
	}

	@Override
	public void warmUpTagAdspace() {
		
		List<PlatformAdspaceMap> tagMaplist = DbDataContainer.getPlatformAdspaceMapAll();// 根据所有的数据
		if (tagMaplist == null) {
			return;
		}
		for (PlatformAdspaceMap am : tagMaplist) {
			try {
				if (am.getStatus() == 1) {
					platformAdspaceMapValueOperations.set(am.toCacheTagIdKey(), am);
				} else if (am.getStatus() != 1) {
					redisTemplate.delete(am.toCacheTagIdKey());
				}
			} catch (Exception e) {
				if (am != null) {
					logger.error("错误：在获取PlatformAdspaceMap_id =" + am.getId() + "数据时发生错误 \n 错误信息：", e);
				}
			}
		}
		
	}

}
